<?php
/* explain:
 * Project:     
 * File:        Job.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class Menu extends Common
{
	public $conHandle;
	public $errorInfo;
	
	private $iUserID;
	
	private $objLog;
	
	function Menu($strLanguage, $userid)
	{
		parent::Common();
		$this->conHandle 	= $this->getConnection("system");
		$this->strLanguage 	= $strLanguage;
		
		$this->objLog 		= new Logs($userid);
		$this->iUserID 		= $userid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getFromID($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM menu WHERE id = $id";
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* 
	 *
	 * @param  string 	groupid		
	 * @param  string 	strOrders	
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @param  string 	url			menu url  
	 * @param  datetime	remark
	 * @param  datetime status
 	 * @return boolean 	true or fail
	 */
	public function add($arrPopedom = array(), $groupid, $strOrders, $name, $url, $remark, $status = 'normal')
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO menu SET groupid = '$groupid', orders='$strOrders', name = '$name', url = '$url', remark = '$remark', status='$status'";
		$rs = mysql_query($sql, $this->conHandle);  
		return $rs ? true: false;
	}
	
	/* modify menu
	 *
	 * @param  string 	id			modify menu id
	 * @param  string 	groupid		
	 * @param  string 	strOrders	
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @param  string 	url			menu url  
	 * @param  datetime	remark
	 * @param  datetime status
 	 * @return boolean 	true or fail
	 */
	public function edit($arrPopedom = array(), $id, $groupid, $strOrders, $name, $url, $remark)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "id=$id");
			
		$sql = "UPDATE menu SET groupid = '$groupid', orders='$strOrders', name = '$name', url = '$url', remark = '$remark' WHERE id = '$id'";
		$rs = mysql_query($sql, $this->conHandle);  
		return $rs ? true: false;
	}
		
	/* delete menu
	 *
	 * @param  int    	id
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
			
		$sql = "DELETE FROM menu WHERE id = $id";
		$rs	 = mysql_query($sql, $this->conHandle);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* list menu
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $condition, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = 'SELECT count(id) FROM menu'.$condition;

		$rs  = mysql_query($sql, $this->conHandle);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = 'SELECT * FROM menu'.$condition;
		else{
			$sql = 'SELECT * FROM menu'.$condition." LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list all custom condition data
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listFromCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrBack = array();
		$sql = "SELECT * FROM menu".$condition;
        $rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}
}
?>
